package com.goods.business.converter;

import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "物资资料")
@RestController
@RequestMapping("/business/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //查询所有物资资料Request
    //business/product/ findProductList? pageNum=1 &pageSize=6& name=asdas& categoryId=&    supplier=&  status=0    &categorys=33,34,37
    //Request Method: GET
    @GetMapping("/findProductList")
    public ResponseBean findProductList(Integer pageNum, Integer pageSize, String name,Integer status,String categorys){
        List<ProductVO> list =  productService.findProductList(name,status,categorys);
        Map<Object, Object> map = new HashMap<>();
        map.put("totle",list.size());
        if(pageNum != null && pageSize != null){
            list = ListPageUtils.page(list, pageSize, pageNum);
            map.put("rows",list);
            return ResponseBean.success(map);
        }else {
            map.put("rows",list);
            return ResponseBean.success(map);
        }
    }



    /*三级分类  http://www.localhost:8989/business/productCategory/categoryTree  Request Method: GET
    * 图片 http://www.localhost:8989/system/upload/image   Request Method: POST
    * business/product/add*/
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        productService.add(productVO);
        return ResponseBean.success();
    }

//    Request URL: http://www.localhost:8989/business/product/edit/37
//Request Method: GET
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        Product product = productService.edit(id);
        return ResponseBean.success(product);
    }
//    .put "business/product/update/"
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductVO productVO){
        System.out.println(productVO.getId());
        productService.update(productVO);
        return ResponseBean.success();
    }
    //加入回收站
//    put("business/product/remove/" + id
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        productService.remove(id);
        return ResponseBean.success();
    }
    //从回收站恢复    put("business/product/back/" + id)
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        productService.back(id);
        return ResponseBean.success();
    }
    //彻底删除  delete("business/product/delete/" + id);
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productService.delete(id);
        return ResponseBean.success();
    }
    //通过Request URL: http://www.localhost:8989/business/product/publish/54
    //Request Method: PUT
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        Boolean publish = productService.publish(id);
        if(publish){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("修改失败");
        }
    }



//    http://www.localhost:8989/business/product/findProducts?pageNum=1&pageSize=6&status=0&categorys=33,34,37&name=as
//Request Method: GET
    @GetMapping("/findProducts")
    public ResponseBean findProducts(Integer pageNum, Integer pageSize, String name,Integer status,String categorys){
        List<ProductVO> list =  productService.findProductList(name,status,categorys);
        Map<Object, Object> map = new HashMap<>();
        map.put("totle",list.size());
        if(pageNum != null && pageSize != null){
            list = ListPageUtils.page(list, pageSize, pageNum);
            map.put("rows",list);
            return ResponseBean.success(map);
        }else {
            map.put("rows",list);
            return ResponseBean.success(map);
        }
    }

   /* business/product/findProductStocks?pageSize=9&pageNum=1&categorys=33,34,37&name=zxc-z     返回一个map findProductList(name,status,categorys);
    Request Method: GET*/
    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(Integer pageSize, Integer pageNum,String categorys,String name){
        List<ProductStockVO> list = productService.findProductLists(name,categorys);
        List<ProductStockVO> page = ListPageUtils.page(list, pageSize, pageNum);
        PageVO<ProductStockVO> productPageVO = new PageVO<>(list.size(), page);
        return ResponseBean.success(productPageVO);
    }

    //business/product/findAllStocks?pageSize=9&pageNum=1&categorys=33,34,37&name=zxc-z
    //Request Method: GET
    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(Integer pageSize, Integer pageNum,String categorys,String name){
        List<ProductStockVO> list = productService.findProductLists(name,categorys);
        /*List<ProductStockVO> page = ListPageUtils.page(list, pageSize, pageNum);
        PageVO<ProductStockVO> productPageVO = new PageVO<>(list.size(), page);*/
        return ResponseBean.success(list);
    }

}
